package org.example.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.constans.WxConstants;
import org.example.dto.GetQRCodeRes;
import org.example.service.WxOfficalApiClient;
import org.example.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class QrcodeController {

    @Autowired
    private WxOfficalApiClient wxOfficalApiClient;

    /**
     * 获取二维码 Ticket
     * <p>
     * https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
     *
     * @return
     */
    @GetMapping("/getQrCode")
    public GetQRCodeRes getQrCode() {
        GetQRCodeRes getQRCodeRes = new GetQRCodeRes();
        Snowflake snowflake = IdUtil.getSnowflake(SnowFlakeUtil.getWorkId(), SnowFlakeUtil.getDataCenterId());
        String scene = String.valueOf(snowflake.nextId());
        String qrCodeUrl = wxOfficalApiClient.getQrCode(WxConstants.APP_ID, WxConstants.APP_SECRET, scene);
        getQRCodeRes.setScene(scene);
        getQRCodeRes.setQrCodeUrl(qrCodeUrl);
        return getQRCodeRes;
    }


}
